/**
 * Created by DELL on 2017/2/23.
 */
export const treeAdapter = function (array, disabledCode = '', isFolderCode = '', platform = 'remove', field = 'orgTypeCode') {
  let result = []
  array.forEach((item) => {
    let disabled = getDisabled(item, disabledCode, platform, field)
    let isFolder = isFolderCode ? item[field].toString() !== isFolderCode : true
    result.push({
      name: item.organName,
      id: item.organId,
      organCode: item.organCode,
      orgTypeCode: item.orgTypeCode,
      orgType: item.orgType,
      children: treeAdapter(item.children, disabledCode, isFolderCode, platform, field),
      isOpen: item.children.length > 0,
      hide: false,
      disabled: disabled,
      isFolder: isFolder
    })
  })
  return result
}
const getDisabled = function (item, disabledCode, platform, field) {
  // orgType时，disabledCode是数字类型，但传过来应该是字符串类型，如‘1,20,21,22’
  let codeArray = disabledCode.split(',')
  if (platform === 'remove') {
    return +item.organId === 1 || (disabledCode ? codeArray.indexOf(item[field].toString()) === -1 : false)
  } else {
    return disabledCode ? (codeArray.indexOf(item[field].toString()) === -1 && +item.organId !== 1) : false
  }
}
export const dataToTree = function (data, tree, disabledCode = '', isFolderCode = '', platform = 'remove', field = 'orgTypeCode') {
  treeReset(tree)
  if (data.length > 1) {
    // 数据大于1个时，增加一个公共节点
    treeCommonNode(tree)
    data.forEach((item) => {
      // platform：‘remove’,organId=1时是全平台，不可选; 'add',organId=1时是全平台，可选
      // 如果没有限制可选类型，则默认为false，都可以选;如果限制，只有可选类型为false，可选
      let disabled = getDisabled(item, disabledCode, platform, field)
      // 如没有限制不可展开类型，则默认为true，都可以展开;如果限制，只有不可展开类型为false，不可展开。isFolderCode传字符串类型。
      let isFolder = isFolderCode ? item[field].toString() !== isFolderCode : true
      tree.children.push({
        name: item.organName,
        id: item.organId,
        organCode: item.organCode,
        orgTypeCode: item.orgTypeCode,
        orgType: item.orgType,
        children: treeAdapter(item.children, disabledCode, isFolderCode, platform, field),
        isOpen: item.children.length > 0,
        hide: false,
        disabled: disabled,
        isFolder: isFolder
      })
    })
  } else if (data.length === 1) {
    tree.name = data[0].organName
    tree.id = data[0].organId
    tree.organCode = data[0].organCode
    tree.orgTypeCode = data[0].orgTypeCode
    tree.orgType = data[0].orgType
    tree.isOpen = data[0].children.length > 0
    tree.disabled = getDisabled(data[0], disabledCode, platform, field)
    tree.isFolder = isFolderCode ? data[0][field].toString() !== isFolderCode : true
    data[0].children.forEach((item) => {
      let disabled = getDisabled(item, disabledCode, platform, field)
      let isFolder = isFolderCode ? item[field].toString() !== isFolderCode : true
      tree.children.push({
        name: item.organName,
        id: item.organId,
        organCode: item.organCode,
        orgTypeCode: item.orgTypeCode,
        orgType: item.orgType,
        children: treeAdapter(item.children, disabledCode, isFolderCode, platform, field),
        isOpen: item.children.length > 0,
        hide: false,
        disabled: disabled,
        isFolder: isFolder
      })
    })
  } else {
    tree.name = '没有查询到数据'
    tree.id = null
    tree.organCode = ''
    tree.orgTypeCode = ''
    tree.orgType = ''
    tree.isFolder = false // 是否有小箭头
    tree.disabled = true
  }
}
// 系统出小差，应该显示为“没有查询到数据”
export const treeReset = function (tree) {
  tree.children.splice(0, tree.children.length)
  tree.name = '没有查询到数据'
  tree.id = null
  tree.organCode = ''
  tree.orgTypeCode = ''
  tree.orgType = ''
  tree.isFolder = false // 是否有小箭头
  tree.disabled = true
}
export const treeCommonNode = function (tree) {
  tree.children.splice(0, tree.children.length)
  tree.name = '公共节点'
  tree.id = null
  tree.organCode = ''
  tree.orgTypeCode = ''
  tree.orgType = ''
  tree.isFolder = true
  tree.isOpen = true
  tree.disabled = true
}
